package _17_函数式编程.Lambda._2_把颜色作为参数;

import _17_函数式编程.Lambda.Apple;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class _2_把颜色作为参数 {

    public static void main(String[] args) {
        List<Apple> appleList = Arrays.asList(
                new Apple("red",123),
                new Apple("green",234),
                new Apple("green",150)
        );

        List<Apple> result;
        result = filterApplesByColor(appleList,"green");
        printApples(result);

        result = filterApplesByColor(appleList,"red");
        printApples(result);
    }

    /**
     * 根据颜色筛选苹果
     * @param inventory
     * @param color
     * @return
     */
    public static List<Apple> filterApplesByColor(List<Apple> inventory, String color){

        List<Apple> result = new ArrayList<>();
        for (Apple apple : inventory){
            if (color.equals(apple.getColor())){
                result.add(apple);
            }
        }
        return result;
    }

    /**
     * 打印苹果信息
     * @param inventory
     */
    public static void printApples(List<Apple> inventory){
        for (Apple apple : inventory){
            System.out.println("苹果的颜色：" + apple.getColor() + "，重量：" + apple.getWeight());
        }
    }
}
